package com.wr.online.test.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author yangyang
 * @version 1.0.0
 * @ClassName RestTemplateUtils
 * @Description
 * @createTime 2021/5/14 15:00
 */
@Component
@Slf4j
public class RestTemplateUtils {

//    @Autowired
    private RestTemplate restTemplate = new RestTemplate();

    /**
     * Http GET请求Url里带参数的请求
     *
     * @param url           请求URL
     * @param respType      返回值类型Class
     * @param pathVariables 参数
     * @param <T>           返回类型
     * @return
     */
    public <T> T httpGetPathVariable(String url, Class<T> respType, String... pathVariables) {
        T result = restTemplate.getForObject(url, respType, pathVariables);
        return restTemplate.getForObject(url, respType, pathVariables);
    }


    /**
     * Http GET请求Url里不带参数的请求
     *
     * @param url            请求URL
     * @param respType       返回类型Class
     * @param urlVariableMap 参数
     * @param <T>            返回类型
     * @return
     */
    public <T> T httpGetUrlVariable(String url, Class<T> respType, Map<String, Object> urlVariableMap) {
        log.info("Http Post请求   MediaType  get  Url={}  param={}", url, JSON.toJSONString(urlVariableMap));
        String urlParams = "";

        if (urlVariableMap != null && urlVariableMap.size() > 0) {

            for (String m : urlVariableMap.keySet()) {
                if (!StrUtil.isEmpty(urlParams)) {
                    urlParams += "&";
                }
                urlParams += m + "={" + m + "}";
            }
        }
        T result = restTemplate.getForObject(url + "?" + urlParams, respType, urlVariableMap);
        log.info("Http Post请求   MediaType  get  Url={}  param={} result={}", url + "?" + urlParams, JSON.toJSONString(urlVariableMap), JSON.toJSONString(result));
        return result;
    }

    /**
     * Http Post请求   MediaType  application/json
     *
     * @param url      请求类型
     * @param respType 返回类型Class
     * @param obj      请求对象信息
     * @param <T>      返回类型
     * @return
     */
    public <T> T httpPostMediaTypeJson(String url, Class<T> respType, Object obj) {
        log.info("Http Post请求   MediaType  application/json  Url={}  param={}", url, JSON.toJSONString(obj));

        long start = System.currentTimeMillis();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);

        HttpEntity<String> formEntity = new HttpEntity<String>(JSON.toJSONString(obj), headers);

        T result = restTemplate.postForObject(url, formEntity, respType);

        log.info("Http Post请求   MediaType  application/json  Url={}  param={} result={} delayTime={}ms", url, JSON.toJSONString(obj), JSON.toJSONString(result), (System.currentTimeMillis() - start) + "");

        return result;
    }


    /**
     * Http Post请求   MediaType  application/x-www-form-urlencoded
     *
     * @param url      请求类型
     * @param respType 返回类型Class
     * @param obj      请求对象信息
     * @param <T>      返回类型
     * @return
     */
    public <T> T httpPostMediaTypeFromData(String url, Class<T> respType, Map<String, Object> obj) {

        log.info("Http Post请求   MediaType  application/x-www-form-urlencoded Url={} param={}", url, JSON.toJSONString(obj));

        LinkedMultiValueMap<String, Object> multiMap = new LinkedMultiValueMap<>();
        if (obj != null && obj.size() > 0) {
            for (String m : obj.keySet()) {
                multiMap.add(m, obj.get(m));
            }
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<LinkedMultiValueMap> req = new HttpEntity<>(multiMap, headers);
        T result = restTemplate.postForObject(url, req, respType);
        log.info("Http Post请求   MediaType  application/x-www-form-urlencoded  Url={}  param={} result={}", url, JSON.toJSONString(obj), JSON.toJSONString(result));

        return result;
    }


}
